//
//  RegisterSecurityCodeVC.m
//  DYLYJ
//
//  Created by JM on 15/10/24.
//  Copyright © 2015年 JM. All rights reserved.
//

#import "RegisterSecurityCodeVC.h"
#import "TYAttributedLabel.h"
#import "DYUIKit.h"
#import "DYRequestAll.h"
@interface RegisterSecurityCodeVC ()
@property(nonatomic,strong)NSMutableAttributedString *string;
@property(nonatomic,strong)UIButton *button;
@property(nonatomic,strong)NSTimer *timer;
@property(nonatomic,assign)NSUInteger timeCount;
@end

@implementation RegisterSecurityCodeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    [self.timeButtonLabel addSubview: [self addTextAttributedLabel]];
//    [self endWaiting];
    RegitsterSendSMSRequest *request = [RegitsterSendSMSRequest request];
    request.DY_captchaId = self.rsponseGetSMSImage.DY_captchaId;
    request.DY_token = self.rsponseGetSMSImage.DY_token;
    request.DY_phone = self.name;
//    request.DY_captchaCode = ;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)endWaiting
{
    self.timeButtonLabel.exclusiveTouch = YES;
//
    self.timeButtonLabel.backgroundColor = [UIColor clearColor];
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:@"没有收到验证码？"];
    self.string = attributedString;
    [attributedString addAttributeTextColor:UIColorFromHexStr(@"#A6937C")];
    [attributedString addAttributeFont:[UIFont systemFontOfSize:15]];
    [self.timeButtonLabel setAttributedText:attributedString];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    self.button = button;
    button.exclusiveTouch = YES;
    [button setTitle:@"重新发送" forState:UIControlStateNormal];
    button.tintColor = UIColorFromHexStr(@"#f39700");
    [button addTarget:self action:@selector(startWaiting:) forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(0, 0, 60, 15);
    [self.timeButtonLabel appendView:button];
    [self.timeButtonLabel sizeToFit];
}


- (void)reloadTimeButtonLabelData {
    NSString * timeCountString =  [NSString stringWithFormat:@"      %lus",(unsigned long)self.timeCount];
    NSMutableAttributedString *string1 = [[NSMutableAttributedString alloc]initWithString:timeCountString];
    [string1 addAttributeTextColor:UIColorFromHexStr(@"#f39700")];
    [string1 addAttributeFont:[UIFont systemFontOfSize:15]];
    NSMutableAttributedString *string2 = [[NSMutableAttributedString alloc]initWithString:@"后才可以重发"];
    [string2 addAttributeTextColor:UIColorFromHexStr(@"#A6937C")];
    [string2 addAttributeFont:[UIFont systemFontOfSize:15]];
    
    NSMutableAttributedString *totalString = [[NSMutableAttributedString alloc] initWithAttributedString:string1];
    [totalString appendAttributedString:string2];
//    [self.timeButtonLabel.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
//    [self.timeButtonLabel.textContainer setText:@""];
    [self.timeButtonLabel setAttributedText:totalString];
//    self.timeButtonLabel.textAlignment = kCTTextAlignmentCenter;//有bug NSTextAlignmentCenter 与 NSTextAlignmentRight反了
    [self.timeButtonLabel sizeToFit];
}

- (void)startWaiting:(id) sender{
    
    self.timeCount =  60;//60秒倒计时
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerRepeat) userInfo:nil repeats:YES];
    
    [self reloadTimeButtonLabelData];
}



- (void)timerRepeat {
    self.timeCount --;
    if(self.timeCount == 0) {
        [self.timer invalidate];
        [self endWaiting];
    } else {
        [self reloadTimeButtonLabelData];
    }
}

- (void)dealloc {
    [self.timer invalidate];
    self.timer = nil;
}

@end
